using FastDevTemplate.DataAccess.Base;

#pragma warning disable CS8618

namespace FastDevTemplate.DataAccess.Entities;

public class Role : Entity<string>
{
    public string Describe { get; set; } = "";
    public ICollection<UserRole> UserRoles { get; set; } = new List<UserRole>();
    public ICollection<RolePermission> RolePermissions { get; set; } = new List<RolePermission>();

    public Role AddPermission(params string[] permissionIDs)
    {
        foreach (var id in permissionIDs)
        {
            if (RolePermissions.All(item => item.PermissionID != id))
                RolePermissions.Add(new RolePermission
                {
                    RoleID = ID,
                    PermissionID = id
                });
        }
        return this;
    }

    public Role RemovePermission(params string[] permissionIDs)
    { 
        foreach (var id in permissionIDs)
        {
            var permission = RolePermissions.FirstOrDefault(item => item.PermissionID == id);
            if (permission != null)
                RolePermissions.Remove(permission);
        }
        return this;
    }
    
    
    public Role SetPermission(params string[] permissionIDs)
    {
        RolePermissions.Clear();
        foreach (var id in permissionIDs)
            RolePermissions.Add(new RolePermission
            {
                RoleID = id,
                PermissionID = id
            });
        return this;
    }
    
}

public class RolePermission
{
    public string RoleID { get; set; }
    public string PermissionID { get; set; }

    public Role Role { get; set; }
    public Permission Permission { get; set; }
}